<?php


function myTeam($uid){
    $myTeams = model('user')->getMyTeam($uid);
    return $myTeams;
}




function paidan($uid,$ppuid,$grade){


    $send_order_to = getSettingValue('send_order_to'.$grade);

    if($send_order_to){
       $array = explode(' ',$send_order_to);

       foreach ($array as $k=>$sendGrade){
            // $sendGrade 派给订单的等级
           $sendGrade = trim($sendGrade);
           if($sendGrade){
               //先查看买家卖家等级是否有记录
               $res = \think\Db::table('jiujiu_shop_paidan')->alias('paidan')
                   ->field(['jiujiu_shop_paidan.*'])
                   ->join('jiujiu_user','jiujiu_user.uid=paidan.uid','LEFT')
                   ->join('jiujiu_order','jiujiu_order.id=paidan.orderId','LEFT')
                   ->where('paidan.buid',$uid)
                   ->where('paidan.ugrade',$sendGrade)
                   ->where('paidan.bgrade',$grade)
                   ->where('jiujiu_user.ppuid',$ppuid)
                   ->where('jiujiu_order.status','>',0)
                   ->where('jiujiu_order.status','<>',8)
                   ->find();
               if($res){
                  return  model('shop')->where('id',$res['shopId'])->find();
               }

           }
       }

        foreach ($array as $k=>$sendGrade){
            // $sendGrade 派给订单的等级
           $sendGrade = trim($sendGrade);
           if($sendGrade){
               //该等级可以接受的派单数
               $get_order = getSettingValue('system_shop_get_paidan'.$sendGrade);
               // 该等级的会员
               $users = model('user')
                   ->where('ppuid',$ppuid)
                   ->where('grade',$sendGrade)
                   ->order('credit desc')
                   ->select();
               foreach ($users as $user ){
                   //会员已经获得的派单数
                   $pds = model('paidan')->getUserPaidanNum($user->uid,$sendGrade);
                   if($get_order-$pds>0){
                       return getUserShop($user->uid);
                   }
               }
           }
       }

        return   getUserShop('system');
    }
    else{
      return   getUserShop('system');
    }
}